package com.yanqu.road.server.cmd.activity.springdinner;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.springdinner.SpringDinnerActivity;
import com.yanqu.road.server.manager.springdinner.SpringDinnerMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C2_SPRINGDINNER_ACTIVITY_OPEN, desc = "红包桌子")
public class CrossSpringDinnerOpenCmd extends CrossCommand {

    private int activityId;
    private long userId;
    private long integral;
    private String message;
    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SpringDinnerProto.CrossOpenRedPacketTableReqMsg reqMsg = SpringDinnerProto.CrossOpenRedPacketTableReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        integral = reqMsg.getIntegral();
        message = reqMsg.getMessage();
        userId = packet.getPlayerId();
        if(reqMsg.hasPlayerBaseData()){
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
            CrossUserMgr.updateUserBaseInfo(userId,userBaseInfo);
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SpringDinnerActivity activity = SpringDinnerMgr.getSpringDinnerActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        ret = activity.openTable(serverId, userId,integral,message);
    }

    @Override
    public void sendData() {
        if(ret != 0){
            SpringDinnerProto.OpenRedPacketTableRespMsg.Builder respMsg = SpringDinnerProto.OpenRedPacketTableRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_SPRINGDINNER_RP_ENTER, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

}
